var PORT = '1234';
var express = require('express');
var app = express();
//监听server
var http = require('http').createServer(app);


app.get('/',function(req,res){
    res.sendFile(__dirname + '/public/index.html');
});
//静态文件
app.use('/',express.static('public'));

http.listen(PORT);


// Chatroom

var numUsers = 0;
const step = '阿哥';
let names = step;
const io = require('socket.io')(http);
io.on('connection', function (socket) {
    // 获取用户发送的信息
    socket.on('send', function (data) {
        if(data){
            if(data.value){
                if(data.value.match(new RegExp(/#(.+)#/))){
                    const groupName = data.value.match(new RegExp(/#(.+)#/))[1];
                    for(var k in io.sockets.sockets){
                        if(io.sockets.sockets[k].username == groupName){
                            io.sockets.sockets[k].emit('msg', {
                                username: io.sockets.sockets[k].username,
                                numUsers:numUsers,
                                message: data
                            });
                        }
                    }

                }else if(data.groupName){
                    if(socket.in(data.groupName)){
                        socket.emit('msg', {
                            groupName:data.groupName,
                            username: socket.username,
                            numUsers:numUsers,
                            message: data
                        });
                    }
                }else{
                    socket.broadcast.emit('msg', {
                        username: socket.username,
                        numUsers:numUsers,
                        message: data
                    });
                }
            }
        }
    });

    // when the client emits 'add user', this listens and executes
    socket.on('login', function (username) {

        if(names.split(step + username + step).length>1){
            socket.emit('errors', {
                code:2001,
                infos: "sorry,名字被注册了或者不合法"
            });
            return;
        }
        names += (username + step);
        // we store the username in the socket session for this client
        socket.username = username;
        socket.join(username);
        ++numUsers;
        socket.emit('online', {
            numUsers: numUsers
        });
        // echo globally (all clients) that a person has connected
        socket.broadcast.emit('online', {
            username: socket.username,
            numUsers: numUsers
        });
    });

    // when the client emits 'typing', we broadcast it to others
    socket.on('typing', function () {
        socket.broadcast.emit('typing', {
            username: socket.username
        });
    });

    // when the client emits 'stop typing', we broadcast it to others
    socket.on('stop typing', function () {
        socket.broadcast.emit('stop typing', {
            username: socket.username
        });
    });

    // when the user disconnects.. perform this
    socket.on('disconnect', function () {
        names = names.replace((socket.username + step),'');
        if(numUsers > 0){
            --numUsers;
        }else{
            numUsers = 0;
        }

        // echo globally that this client has left
        socket.broadcast.emit('offline', {
            username: socket.username,
            numUsers: numUsers
        });
    });
});

console.log("Server runing at port: " + PORT + ".");
